package com.eateateat.controller;


import com.eateateat.entity.CarMenu;
import com.eateateat.service.CarService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController  // @Controller+@ResponseBody @ResponseBody注解来返回一个JSON对象
@RequestMapping("/cart")

public class CarMenuController {
    Logger logger = Logger.getLogger(CarMenuController.class);
//    @Qualifier("carServiceImpl2")
    @Autowired
    CarService cartService;
    @GetMapping("/addcart")

    String addcart(HttpServletRequest request, CarMenu cmenu, @RequestParam Map map) throws IOException {
        logger.debug(map);//{lxmenuid=8}
        int lxmenuid= Integer.parseInt((String)map.get("lxmenuid")) ; //
        Map m = (Map) request.getSession().getAttribute("islogin");
        logger.debug(m);//{lxmemid=哒玲玲, lxmemalias=1, lxmempass=Yi123456}根据当前用户
        map.put("lxmemid", m.get("lxmemid"));
//        String lxmemid= (String) map.get("lxmemid");
//        cmenu.setLxmemid(lxmemid);
//        cmenu.getLxmenuid(lxmenuid);
        logger.debug(map);//{lxmenuid=58, lxname=测试38, lxprice=38, lxmemid=哒玲玲}
        request.getSession().setAttribute("cainame",m.get("lxname"));
        Map cainame= (Map) request.getSession().getAttribute("cainame");
        logger.debug(cainame);

        if (cartService.addCart(map)) {
            logger.debug("success");
            return "success";
        } else return "failure";

    }

//    @GetMapping("/delete/{lxmenuid}")
////spring风格 前端传的是	url : "menu2/delete/"+menuid,//Spring风格
//    String delete(HttpServletResponse response, HttpServletRequest request, @PathVariable int lxmenuid,@RequestParam Map map) throws IOException {
//        map= (Map) request.getSession().getAttribute("islogin");
//        logger.debug(map);
//        logger.debug(lxmenuid);
//        map.put("lxmenuid",lxmenuid);
//        logger.debug(map);
////        String lxmemid= (String) request.getSession().getAttribute("islogin");
////        map.put("lxmemid",lxmemid);
//        logger.debug(map);
//        if (cartService2.delete(map)) {
//            return ("success!");
//        } else {
//            return ("delete failure!");
//
//        }
//    }
@GetMapping("/delete")
String delete(HttpServletRequest request, HttpServletResponse response) throws IOException {

//    String celxmenuid = request.getParameter("lxmenuid");
//    logger.debug(celxmenuid);
    int lxmenuid = Integer.parseInt(request.getParameter("lxmenuid"));
    logger.debug(lxmenuid);
    Map lxmemid = (Map) request.getSession().getAttribute("islogin");
    logger.debug(lxmemid);
    if (cartService.delete((String) lxmemid.get("lxmemid"),lxmenuid)){
        return "success";
    } else {
        return "failure";
    }
}
    @PostMapping("/select")
    List select(HttpServletRequest request, HttpServletResponse response,@RequestParam Map map) throws IOException {
        String lxname = request.getParameter("lxname");
        logger.debug(lxname);
        if (lxname == null) {
            lxname = "";
        }
        logger.debug(map);
        Map m = (Map) request.getSession().getAttribute("islogin");
        logger.debug(m);//{lxmemid=哒玲玲, lxmemalias=1, lxmempass=Yi123456}根据当前用户

        map.put("lxmemid", m.get("lxmemid"));
        String lxmemid= (String) map.get("lxmemid");
        logger.debug(lxmemid);

        List result=cartService.select(lxname, lxmemid);
        logger.debug(result);//[{lxmemid=哒玲玲, quantity=1, lxmenuid=57, lxprice=22.0, lxname=糖醋排骨}, {lxmemid=哒玲玲, quantity=1, lxmenuid=58, lxprice=38.0, lxname=测试38}, {lxmemid=哒玲玲, quantity=1, lxmenuid=49, lxprice=21.0, lxname=12345}, {lxmemid=哒玲玲, quantity=1, lxmenuid=4, lxprice=55.0, lxname=菜5}, {lxmemid=哒玲玲, quantity=1, lxmenuid=9, lxprice=6.0, lxname=菜六}]
        map.put("lxname", m.get("lxname"));

        return result;
    }


}
